var islogin = '';

function loginmodal() {
    islogin = 'true';
    $("#username").val('');
    $("#password").val('');
    $("#rpassword").val('');
    document.getElementById('rpassdiv').style.display = 'none';
    document.getElementById('logintipdiv').style.display = 'block';
    document.getElementById('registtipdiv').style.display = 'none';
    document.getElementById('loginbtndiv').style.display = 'block';
    document.getElementById('registerdiv').style.display = 'none';
    document.getElementById('logindiv').style.display = 'block';
    $("#bottomdiv").css("margin-top", "80px");
    $("#registwaring").html("<div>6位起且字符数字字母至少两者</div>")
    $("#loginwaring").html("");
}

function closelogin() {
    document.getElementById('logindiv').style.display = 'none';
}

function tologin() {

    var username = $("#username").val();
    var password = $("#password").val();
    $.ajax({
        type: 'post',
        url: '/tologin',
        contentType: "application/json",
        data: JSON.stringify({
            username: username,
            password: password
        }),
        success: function (rescode) {
            if (rescode == "200") {
                swal("去登陆了哦", {
                    icon: "success",
                });
                window.location.href = '/login';
            } else {
                $("#loginwaring").html("用户或密码出错");
            }
        },
        error: function () {
            alert("失败")
        }
    });
}

function register() {
    islogin = 'false';
    $("#username").val('');
    $("#password").val('');
    $("#bottomdiv").css("margin-top", "40px");
    document.getElementById('rpassdiv').style.display = 'block';
    document.getElementById('registtipdiv').style.display = 'block';
    document.getElementById('registerdiv').style.display = 'block';
    document.getElementById('logintipdiv').style.display = 'none';
    document.getElementById('loginbtndiv').style.display = 'none';
}


function toregister() {
    $.ajax({
        type: 'post',
        url: '/toregister',
        contentType: "application/json",
        data: JSON.stringify({
            username: $("#username").val(),
            password: $("#password").val()
        }),
        success: function (rescode) {
            if (rescode == "200") {
                window.location.href = '/login';
            } else if (rescode == "400") {
                $('#registwaring').html("<div style=\"color: red\">用户名存在</div>")
            } else {
                $('#registwaring').html("<div style=\"color: red\">注册失败</div>")
            }
        },
        error: function () {
            alert("失败")
        }
    });
}

var pattern = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{6,}$/
$(document).ready(function () {
    $("#password").keyup(function () {
        if (islogin == "false") {
            if ($("#password").val().length < 6)
                $('#registwaring').html("<div style=\"color: red\">长度至少6位</div>")
            else if (!pattern.test($("#password").val()))
                $('#registwaring').html("<div style=\"color: red\">不符合2/3规则</div>")
            else
                $('#registwaring').html("<div style=\"color: green\">正确</div>")
        }
    });

    $("#rpassword").keyup(function () {
        if (islogin == "false") {
            if ($("#rpassword").val() != $("#password").val())
                $('#registwaring').html("<div style=\"color: red\">输入不一致</div>")
            else
                $('#registwaring').html("<div style=\"color: green\">正确</div>")
        }
    });
})

function avtorliston() {
    $("#avtorimg").click();
}

function avtorlistoff() {
    $("#avtorimg").click();
}
